<!DOCTYPE html>
<html>
<head>
<title>LDS service mgmt. console</title>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type/>
<link href='https://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'/>
<link rel=stylesheet type=text/css href="https://eagri.cz/ssl/nosso-app/uhul/PRX/CD/layout.jquery-dev.net/lib/css/layout-default-latest.css"/>
<link rel=stylesheet type=text/css href="lds.css"/>

<!-- LAYOUT v 1.3.0 -->
<script type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script type=text/javascript src="https://eagri.cz/ssl/nosso-app/uhul/PRX/CD/layout.jquery-dev.net/lib/js/jquery.layout-latest.min.js"></script>
<script type=text/javascript src="js/jquery-fieldselection.min.js"></script>

<script type=text/javascript>

/*
simple xml formatter
*/
function formatXml(xml) {
	var formatted = '';
	var reg = /(>)(<)(\/*)/g;
	xml = xml.replace(reg, '$1\r\n$2$3');
	var pad = 0;
	jQuery.each(xml.split('\r\n'), function(index, node) {
	    var indent = 0;
	    if (node.match( /.+<\/\w[^>]*>$/ )) {
	        indent = 0;
	    } else if (node.match( /^<\/\w/ )) {
	        if (pad != 0) {
	            pad -= 1;
	        }
	    } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
	        indent = 1;
	    } else {
	        indent = 0;
	    }
	
	    var padding = '';
	    for (var i = 0; i < pad; i++) {
	        padding += '  ';
	    }
	
	    formatted += padding + node + '\r\n';
	    pad += indent;
	});
	
	return formatted;
}




function toggleLiveResizing () {
	$.each( $.layout.config.borderPanes, function (i, pane) {
		var o = myLayout.options[ pane ];
		o.livePaneResizing = !o.livePaneResizing;
	});
};



$(document).on({
    ajaxStart: function() {
        $(this.body).addClass("loading");
    },
    ajaxStop: function() {
        $(this.body).removeClass("loading");
    }
});

var myLayout;

$(document).ready(function () {

	// this layout could be created with NO OPTIONS - but showing some here just as a sample...
	// myLayout = $('body').layout(); -- syntax with No Options

	myLayout = $('body').layout({

	//	reference only - these options are NOT required because 'true' is the default
		closable:					true	// pane can open & close
	,	resizable:					true	// when open, pane can be resized 
	,	slidable:					false	// when closed, pane can 'slide' open over other panes - closes on mouse-out
	,	livePaneResizing:			true

	//	some resizing/toggling settings
	, north__size:		32
	,	north__slidable:			false	// OVERRIDE the pane-default of 'slidable=true'
	,	north__resizable:			false	// OVERRIDE the pane-default of 'slidable=true'
	,	north__togglerLength_closed: '100%'	// toggle-button is full-width of resizer-bar
	,	north__spacing_open:		0
	,	north__spacing_closed:		20		// big resizer-bar when open (zero height)

	//	some pane-size settings
	,	center__minHeight:				150
	,	center__size:					"auto"
	//,	east__minSize:				200
	//,	east__maxSize:				.5 // 50% of layout width
	,	center__minWidth:			100
	,	south__size:					400
	,	south__minHeight:				100
	//	some pane animation settings
	//,	west__animatePaneSizing:	false
	//,	west__fxSpeed_size:			"fast"	// 'fast' animation when resizing west-pane
	//,	west__fxSpeed_open:			1000	// 1-second animation when opening west-pane
	//,	west__fxSettings_open:		{ easing: "easeOutBounce" } // 'bounce' effect when opening
	//,	west__fxName_close:			"none"	// NO animation when closing west-pane

	//	enable showOverflow on west-pane so CSS popups will overlap north pane
	//,	west__showOverflowOnHover:	true

	//	enable state management
	,	stateManagement__enabled:	false // automatic cookie load & save enabled by default

	,	showDebugMessages:			true // log and/or display messages from debugging & testing code
	});

	// if there is no state-cookie, then DISABLE state management initially
	//var cookieExists = !$.isEmptyObject( myLayout.readCookie() );
	//if (!cookieExists) toggleStateManagement( true, false );
	
	
	
  $("button#processBtn").click(function(){
  
  	var dsmlReq = "";
  	var range = $("textarea#dsmlReq").getSelection();
  	
  	if(range.text.length > 0)
  		dsmlReq = range.text
  		
  	else
  		dsmlReq = jQuery("textarea#dsmlReq").val()
  	
  	reqData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\<s:Body><batchRequest xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:oasis:names:tc:DSML:2:0:core\"\ requestID=\"Batch of search requests\" responseOrder=\"Unordered\">"
					 + dsmlReq
					 + "</batchRequest></s:Body></s:Envelope>";
					 
    $.ajax({
	    url: 'LdConnection.svc'
	    ,type: 'POST'
	    ,async: true
	    ,data: reqData
	    ,headers: {
	        //"Accept-Encoding": 'gzip, deflate',
	        //"Content-Type": 'text/xml; charset=utf-8',
	        SOAPAction: "\"Process\""
	    }
	    ,contentType: 'text/xml; charset=utf-8'
	    ,dataType: 'xml'
	    ,timeout: 90000
	    ,success: function(xmlData, status, xhr)
	    {
	        //console.info(xmlData);
	        //alert("Data: " + xmlData.toString());
	        jQuery("textarea#dsmlRes").val(formatXml(xhr.responseText))
	        //jQuery("#output").text(formatXml(xhr.responseText))
	        //alert(xhr.responseText);
	        //jQuery("#output").innerHtml = "<test><g>asda</g><g>adfsdf</g></test>"
	        //alert(xhr.responseXML);
	        //var data = $(xmlData).find('batchResponse').text();
	        //jQuery("#output").html(data);
	        //prettyPrint();
	    }
	    ,error: function (xhr, ajaxOptions, thrownError) {
        //alert(xhr.status);
        alert(xhr.status + thrownError);
      }
		});
		return false;
  });
  $("button#addBtn").click(function(){
		var operation = "";
  	
		switch($("#addCmdCbo").val()){
			case "search":
				operation =	"<searchRequest	dn=\"{@replaceDN}\" requestID=\"some search...\" scope=\"wholeSubtree\" derefAliases=\"neverDerefAliases\" typesOnly=\"false\"><filter><and><present name=\"objectClass\"/><equalityMatch name=\"objectClass\"><value>data acl</value></equalityMatch></and></filter><attributes></attributes></searchRequest>"
				break;
				
			case "modify":
				operation =	"<modifyRequest dn=\"{@replaceDN}\"><modification name=\"description\" operation=\"replace\"><value>This is the new description</value></modification><modification name=\"description\" operation=\"add\"><value>This is the new description</value></modification><modification name=\"description\" operation=\"delete\"><value>This is the new description</value></modification></modifyRequest>"
				break;
				
			case "add":
				operation =	"<addRequest dn=\"{@replaceDN}\"><attr name=\"objectClass\"><value>top</value><value>person</value></attr><attr name=\"uid\"><value>test.user</value></attr></addRequest>"
				break;
				
			case "modifyDn":
				operation =	"<modDNRequest dn=\"{@replaceDN}\" newrdn=\"fill new value here...\" deleteoldrdn=\"true\" newSuperior=\"fill new value here...\" />"
				break;
				
			case "delete":
				operation =	"<delRequest dn=\"{@replaceDN}\"/>"
				break;
				
			case "compare":
				operation =	"<compareRequest dn=\"{@replaceDN}\"> <assertion name=\"attrib name...\"><value>some value...</value></assertion></compareRequest>"
				break;
				
		}
		
		operation = operation.replace("{@replaceDN}", $("#dnTxt").val());
		
		$("textarea#dsmlReq").val(
					$("textarea#dsmlReq").val()
					+ formatXml(operation)
				);
    
		return false;
  });
  
  $("button#addUuidBtn").click(function(){
  	var data = "";
  	
  	$.ajax({
	    //url: '../../../../../public/app/uhul/PRX/CD/www.fileformat.info/tool/guid.htm?count=1&format=text&hyphen=y&uppercase=y'
	    url: 'https://www.fileformat.info/tool/guid.htm?count=1&format=text&hyphen=y&uppercase=y'
	    ,type: 'get'
	    ,async: true
	    ,dataType: 'text'
	    ,timeout: 5000
	    ,success: function(xmlData, status, xhr)
	    {
	    	data = "cn=" + xhr.responseText + ",";
	      $("#dnTxt").val(
	      	data + $("#dnTxt").val()
				);
	    }
	    ,error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
		});
  
		return false;
  });

});

</script>

</head>
<body>
  <!-- manually attach allowOverflow method to pane -->
  <div
    class=ui-layout-north onmouseover="myLayout.allowOverflow('north')" 
    onmouseout=myLayout.resetOverflow(this) style="margin:0px;padding:0px">
    <div id="toolbar">
			<span> add command:</span>
			<select id="addCmdCbo">
				<option value="search">search</option>
				<option value="compare">compare</option>
				<option value="add">add</option>
				<option value="delete">delete</option>
				<!--<option value="delete tree">delete tree</option>-->
				<option value="modify">modify</option>
				<option value="modifyDn">modifyDn</option>
			</select>
			<span> for </span>
			<button id="addUuidBtn">newId</button>
			<span> DN: </span>
			<input id="dnTxt" type="text" value="cn=">
			<button id="addBtn">Add</button>
			<button id="processBtn">Process</button>
		</div>
  </div>
  <div class=ui-layout-center style="margin:0px;padding:0px">
  <textarea id="dsmlReq"></textarea>
  </div>
  <div class="ui-layout-south" style="margin:0px;padding:0px">
  <textarea id="dsmlRes"></textarea>
  </div>
  
  <div class="modal"></div>
  
</body>
</html>
